package org.hongqi.common.senpec.file.fileInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hongqi.common.senpec.base.mybatisplus.IBaseServiceImpl;
import org.hongqi.common.senpec.base.sevlet.Result;
import org.hongqi.common.senpec.file.cosClient.CosClientService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;

@Service
@Slf4j
@RequiredArgsConstructor
public class FileInfoServiceImpl extends IBaseServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {

    private final FileInfoMapper fileInfoMapper;

    private final CosClientService cosClientService;

    @Override
    public FileInfo getFileInfoByKeyword(String keyword) {
        LambdaQueryWrapper<FileInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileInfo::getKeyword, keyword);
        return fileInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public FileInfo uploadOneFile(String keyword, MultipartFile file) {
        FileInfo fileInfoByKeyword = getFileInfoByKeyword(keyword);
        FileInfo fileInfo;
        if (fileInfoByKeyword == null) {
            log.info("开始上传文件。。");
            fileInfo = handleInsertFile(keyword, file);
            log.info("完成上传文件。。");
        } else {
            log.info("开始更新上传文件。。");
            fileInfo = handleUpdateFile(keyword, file);
            log.info("完成更新上传文件。。");
        }
        return fileInfo;
    }

    @Override
    public FileInfo handleInsertFile(String keyword, MultipartFile file) {
        FileInfo fileInfo = cosClientService.uploadInputStream(keyword, file);
        fileInfoMapper.insert(fileInfo);
        return fileInfo;
    }

    @Override
    public FileInfo handleUpdateFile(String keyword, MultipartFile file) {
        FileInfo fileInfo = fileInfoMapper.selectByKeyword(keyword);
        cosClientService.deleteFile(fileInfo.getBucket(), fileInfo.getFileKey());
        FileInfo uploaded = cosClientService.uploadInputStream(keyword, file);
        uploaded.setId(fileInfo.getId());
        fileInfoMapper.updateById(uploaded);
        return fileInfoMapper.selectById(uploaded.getId());
    }

    @Override
    public FileInfo selectByKeyword(String keyword) {
        return fileInfoMapper.selectByKeyword(keyword);
    }

    @Override
    public FileInfo appendOneFile(String keyword, MultipartFile file) {
        log.info("开始上传文件。。");
        FileInfo fileInfo = handleInsertFile(keyword, file);
        log.info("完成上传文件。。");
        return fileInfo;
    }

    @Override
    public Result<Collection<FileInfo>> myListByKeyword(String keyword) {
        Collection<FileInfo> fileInfos = fileInfoMapper.listByKeyword(keyword);
        return Result.ok(fileInfos);
    }

    @Override
    public Result<?> myRemoveById(Long id) {
        FileInfo fileInfo = fileInfoMapper.selectById(id);
        cosClientService.deleteFile(fileInfo.getBucket(), fileInfo.getFileKey());
        fileInfoMapper.deleteById(id);
        fileInfoMapper.myDeleteById(id);
        return Result.ok();
    }
}
